package com.firewolf.rbac.verifycode;

import com.firewolf.rbac.verifycode.ImageCodeGenerator;
import com.firewolf.rbac.verifycode.ImageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 作者：刘兴
 * 时间:19/3/6
 **/
@RestController
@RequestMapping("/verifycode")
public class VerifyCodeController {

    public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";

    @Autowired
    private ImageCodeGenerator verifyCodeGenerator;

    @GetMapping("/image")
    public void imageCode(HttpSession session, HttpServletResponse response) throws Exception {
        ImageCode imageCode = verifyCodeGenerator.generatorImageCode();
        //保存验证码到session，登录的时候校验
        session.setAttribute(SESSION_KEY, imageCode);
        ImageIO.write(imageCode.getImage(), "JPEG", response.getOutputStream());
    }
}
